package com.android.wellcherish.workclock.view;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * @author 张文琛
 * @date 2020/3/22 15:43
 */
public class LineItemDecoration extends RecyclerView.ItemDecoration {
    private int height, startPadding, endPadding;
    private Paint paint;

    public LineItemDecoration(int height, int startPadding, int endPadding) {
        this.height = height;
        this.startPadding = startPadding;
        this.endPadding = endPadding;
        paint = new Paint();
        paint.setColor(Color.BLACK);
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view,
        @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        // outRect 是 item 外的包裹矩形。是距其他矩形的偏移量
        super.getItemOffsets(outRect, view, parent, state);
        outRect.bottom = height;
    }

    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent,
        @NonNull RecyclerView.State state) {
        int count = parent.getChildCount();
        if(count <= 0) {
            return;
        }
        int left, top, right, bottom;
        for(int i = 0; i < count; i++) {
            if(i == count - 1) {
                // 最后一个不绘制
                continue;
            }
            View child = parent.getChildAt(i);
            left = child.getLeft() + startPadding;
            right = child.getRight() - endPadding;
            top = child.getBottom();
            bottom = child.getBottom() + height;
            // 画主体
            c.drawRect(left, top, right, bottom, paint);
        }
    }
}
